package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EntityDevice;
import com.ruoyi.system.service.IEntityDeviceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 器材Controller
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Controller
@RequestMapping("/system/device")
public class EntityDeviceController extends BaseController
{
    private String prefix = "system/device";

    @Autowired
    private IEntityDeviceService entityDeviceService;

    @RequiresPermissions("system:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询器材列表
     */
    @RequiresPermissions("system:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EntityDevice entityDevice)
    {
        startPage();
        List<EntityDevice> list = entityDeviceService.selectEntityDeviceList(entityDevice);
        return getDataTable(list);
    }

    /**
     * 导出器材列表
     */
    @RequiresPermissions("system:device:export")
    @Log(title = "导出器材列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EntityDevice entityDevice)
    {
        List<EntityDevice> list = entityDeviceService.selectEntityDeviceList(entityDevice);
        ExcelUtil<EntityDevice> util = new ExcelUtil<EntityDevice>(EntityDevice.class);
        return util.exportExcel(list, "device");
    }

    /**
     * 新增器材
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存器材
     */
    @RequiresPermissions("system:device:add")
    @Log(title = "新增器材", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(EntityDevice entityDevice)
    {
        return entityDeviceService.insertEntityDevice(entityDevice);
    }

    /**
     * 修改器材
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EntityDevice entityDevice = entityDeviceService.selectEntityDeviceById(id);
        mmap.put("entityDevice", entityDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存器材
     */
    @RequiresPermissions("system:device:edit")
    @Log(title = "修改器材信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(EntityDevice entityDevice)
    {
        return entityDeviceService.updateEntityDevice(entityDevice);
    }

    /**
     * 删除器材
     */
    @RequiresPermissions("system:device:remove")
    @Log(title = "删除器材", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(entityDeviceService.deleteEntityDeviceByIds(ids));
    }

    /**
     *
     */
    @PostMapping( "/selectEntityDeviceBySerialNumber")
    @ResponseBody
    public AjaxResult selectEntityDeviceBySerialNumber(String serialNumber) {
        AjaxResult result = new AjaxResult();
        result.put("code",0);
        result.put("value",entityDeviceService.selectEntityDeviceBySerialNumber(serialNumber));
        return result;
    }
}
